﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;

namespace Common
{
    /// <summary>
    /// JS提示信息操作
    /// </summary>
    public class WebAlert
    {
        static System.Web.UI.Page Page
        {
            get
            {
                return HttpContext.Current.Handler as System.Web.UI.Page;
            }
        }

        /// <summary>
        /// 弹出指定的消息,并使框架页所有转向指定的URL
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="url">URL</param>
        public static void TopAlert(string msg, string url)
        {
            TopAlert(new string[] { msg }, url);
        }

        /// <summary>
        /// 弹出指定的消息,并使框架页所有转向指定的URL
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="url">URL</param>
        public static void TopAlert(string[] msg, string url)
        {
            url = url ?? "";
            Javascript js = new Javascript();
            js.AppendAlert(msg);
            js.Append(string.Format("top.location.href='{0}';", url.Replace("'", "\\'")));
            HttpContext.Current.Response.Write(js.GetString());
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 弹出指定的消息
        /// </summary>
        /// <param name="msg">消息</param>
        public static void Alert(string msg)
        {
            Alert(new string[] { msg });
        }

        /// <summary>
        /// 弹出指定的消息并刷新页面
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="refresh">是否刷新</param>
        public static void Alert(string msg,bool refresh)
        {
            Alert(msg,Page.Request.RawUrl);
        }

        /// <summary>
        /// 弹出指定的消息
        /// </summary>
        /// <param name="msg">消息</param>
        public static void Alert(string[] msg)
        {
            Javascript js = new Javascript();
            js.AppendAlert(msg);
            HttpContext.Current.Response.Write(js.GetString());
        }
        /// <summary>
        /// 弹出指定的消息,并返回指定的URL
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="url">URL</param>
        public static void Alert(string[] msg, string url)
        {
            Javascript js = new Javascript();
            js.AppendAlert(msg);
            js.AppendGoUrl(url);
            HttpContext.Current.Response.Write(js.GetString());
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 弹出指定的消息,并返回指定的URL
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="url">URL</param>
        public static void Alert(string msg, string url)
        {
            Alert(new string[] { msg }, url);
        }
        /// <summary>
        /// 弹出指定消息后,返回上页
        /// </summary>
        /// <param name="msg">消息</param>
        public static void AlertBack(params string[] msg)
        {

            Javascript js = new Javascript();
            js.AppendAlert(msg);
            js.AppendBack(1);
            HttpContext.Current.Response.Write(js.GetString());
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 执行提交信息组并关闭窗体
        /// </summary>
        /// <param name="msg">信息</param>
        public static void AlertClose(params string[] msg)
        {
            Javascript js = new Javascript();
            js.AppendAlert(msg);
            js.AppendWindowClose();
            HttpContext.Current.Response.Write(js.GetString());
            HttpContext.Current.Response.End();
        }
    }
}
